home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ultra250.zip / UW_TUT8.C < prev    next >
Text File  |  1992-11-02  |  19KB  |  490 lines

  1. /****************************************************************************/
  2. /* UW_TUT8.C                                                                */
  3. /*                                                                          */
  4. /* NOTE: THIS FILE IS PUBLIC DOMAIN AND MAY BE MODIFIED AND USED AT WILL    */
  5. /*                                                                          */
  6. /* Now we mouse support for the windows and data entry selection...         */
  7. /* To add mouse we use a cute little trick - we see where the mouse cursor  */
  8. /* is when a button is pressed and map the area to a key.  Processing then  */
  9. /* proceeds just as though you pressed the key.  There are other ways to    */
  10. /* interface to the mouse, this is just one simple method.                  */
  11. /*                                                                          */
  12. /*                                                         Dr. Boyd Gafford */
  13. /*                                                         Kevin Huck       */
  14. /*                                                         EnQue Software   */
  15. /*                                                         09/16/92         */
  16. /****************************************************************************/
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #include <io.h>
  20. #ifndef __TURBOC__
  21. #include <sys\types.h>
  22. #endif
  23. #include <sys\stat.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include "uw.h"                           /* include the necessary headers  */
  27.  
  28. #define MAX_CUST 100
  29.  
  30. typedef struct cust
  31. {
  32.   int status;
  33.   int cust_no;
  34.   char business[34];
  35.   char name[34];
  36.   char addr[34];
  37.   char city[34];
  38.   char state[4];
  39.   char zip[10];
  40.   char phone[16];
  41.   char fax[16];
  42.   char date[10];
  43.   char memo[34];
  44.   char unused[26];                                /* round out to 256 bytes */
  45. } CUST;
  46.  
  47. /*----------------------- global window variables --------------------------*/
  48. WINDOW   Desk_wn, Window1;
  49. CUST Customers[MAX_CUST];
  50. char Fname[33];
  51.  
  52. MENU    Top_menu, *Top_mnp = &Top_menu;
  53. MENU    Files_menu, Edit_menu, Print_menu;
  54. MENU    *Drop_mnps[3];
  55.  
  56. PRINT   Print;
  57.  
  58. /*-------------------------------- prototypes ------------------------------*/
  59. int disp_time(void);
  60. void disp_cust(CUST *cp, WINDOW *wnp);
  61. int file_load(CUST *customers);
  62. int file_save(CUST *customers);
  63. int get_fname(char *fname);
  64. void print_cust(CUST *cp, PRINT *p);
  65.  
  66. /*********/
  67. /* ~main */
  68. /*       ********************************************************************/
  69. /*  Demonstrate data entry capability...                                    */
  70. /****************************************************************************/
  71. int main()
  72. {
  73.   int i, ret_val, cust = 0, end_flag = 0, print_stat = 0;
  74.   WINDOW *wnp;
  75.   CUST *cp;
  76.   uchar back_att  = (LIGHTGRAY << 4) | BLACK,
  77.         bdr_att   = (LIGHTGRAY << 4) | BLACK,
  78.         csr_att   = (CYAN << 4) | YELLOW,
  79.         first_att = (LIGHTGRAY << 4) | RED;
  80.  
  81.   wnp = &Window1;                         /* set local window pointer       */
  82.   init_video(80, 25);                     /* init video for 80 x 25 screen  */
  83.   init_clock(0x3333);                     /* init clock irq at 91 tics/sec  */
  84.   init_mouse();                           /* init mouse if available        */
  85.  
  86.   wn_create(0, 0, V_cols-1, V_rows-1, NO_BDR, WN_NORMAL, &Desk_wn);
  87.   link_window(&Desk_wn);
  88.  
  89.   /*------------------------ create the menu system ------------------------*/
  90.   Drop_mnps[0] = &Files_menu;
  91.   Drop_mnps[1] = &Edit_menu;
  92.   Drop_mnps[2] = &Print_menu;
  93.  
  94.   menu_create(0, 0, V_cols - 1, 0, M_HORIZONTAL,
  95.               back_att, bdr_att, csr_att, first_att,
  96.               NO_BDR, WN_NORMAL, Top_mnp);
  97.   item_add( "   Files   ", 1, 3, &Top_menu );
  98.   item_add( "   Edit    ", 2, 3, &Top_menu );
  99.   item_add( "   Print   ", 8, 3, &Top_menu );
  100.  
  101.   menu_create(0, 1, 14, 5, M_VERTICAL,
  102.     back_att, bdr_att, csr_att, first_att,
  103.     SGL_BDR, WN_NORMAL, Drop_mnps[0]);
  104.   item_add( " Load File", 4, 1, &Files_menu );
  105.   item_add( " Save File", 5, 1, &Files_menu );
  106.   item_add( "   Quit   ", 3, 3, &Files_menu );
  107.  
  108.   menu_create(11, 1, 32, 4, M_VERTICAL,
  109.     back_att, bdr_att, csr_att, first_att,
  110.     SGL_BDR, WN_NORMAL, Drop_mnps[1]);
  111.   item_add( " Clear Current", 6, 7, &Edit_menu );
  112.   item_add( " Clear All    ", 7, 7, &Edit_menu );
  113.  
  114.   menu_create(22, 1, 43, 4, M_VERTICAL,
  115.     back_att, bdr_att, csr_att, first_att,
  116.     SGL_BDR, WN_NORMAL, Drop_mnps[2]);
  117.   item_add( " Print Current", 9, 7, &Print_menu );
  118.   item_add( " Print All    ", 10, 7, &Print_menu );
  119.  
  120.   set_idle_func(disp_time);               /* set background clock function  */
  121.  
  122.   wn_create(5, 5, 75, 20, SLD_BDR, WN_POPUP, wnp);
  123.   wn_color(YELLOW, BLUE, wnp);            /* change the window colors       */
  124.   wn_bdr_color(WHITE, BLUE, wnp);         /* change the border's colors     */
  125.   link_window(wnp);
  126.  
  127.   /*------------- initialize first customer as EnQue Software --------------*/
  128.   cp = &Customers[0];
  129.   strcpy(cp->business, "EnQue Software"); 
  130.   strcpy(cp->name, "Kevin Huck & Boyd Gafford");  
  131.   strcpy(cp->addr, "Rt. 1 Box 116C"); 
  132.   strcpy(cp->city, "Pleasant Hill");  
  133.   strcpy(cp->state, "MO");  
  134.   strcpy(cp->zip, "64080"); 
  135.   strcpy(cp->phone, "(816)987-2515"); 
  136.   strcpy(cp->fax, "(816)987-2515");      
  137.   strcpy(cp->date, "09/11/92");      
  138.   strcpy(cp->memo, "BBS 816-358-8990"); 
  139.  
  140.   /*------------------------ initialize the printer ------------------------*/
  141.   if( init_printer("LPT1", NULL, 2048L, 2048L, &Print) )
  142.     print_stat = 1;
  143.  
  144.   menu_set(Top_mnp);
  145.   wn_plst(CENTERED, 3, "Use cursor keypad to select customer", &Desk_wn);
  146.   wn_plst(CENTERED, 4, "or click on cursor buttons at bottom of screen", &Desk_wn);
  147.   wn_plst(CENTERED, 22, "<Up> <Dn>  <PgUp> <PgDn>  <Home> <End>", &Desk_wn);
  148.   while(!end_flag)
  149.   {
  150.     cp = &Customers[cust];
  151.     mv_cs(1,1, wnp);
  152.     wn_printf(wnp, "Customer:%3d", cust+1);
  153.     disp_cust(cp, wnp);
  154.     m_show();
  155.     wait_event();
  156.     m_hide();
  157.     if( Event.is_mouse )
  158.     {
  159.       if( range(0,Event.m_x,11) && (Event.m_y == 0) )
  160.         Event.key = KEY_ALT_F;
  161.       else if( range(11,Event.m_x,22) && (Event.m_y == 0) )
  162.         Event.key = KEY_ALT_E;
  163.       else if( range(22,Event.m_x,33) && (Event.m_y == 0) )
  164.         Event.key = KEY_ALT_P;
  165.  
  166.       else if( range( 7,Event.m_x,47) && (Event.m_y == 9) )
  167.         Event.key = 'B';
  168.       else if( range( 7,Event.m_x,47) && (Event.m_y == 10) )
  169.         Event.key = 'N';
  170.       else if( range( 7,Event.m_x,47) && (Event.m_y == 11) )
  171.         Event.key = 'A';
  172.       else if( range( 7,Event.m_x,47) && (Event.m_y == 12) )
  173.         Event.key = 'C';
  174.       else if( range(50,Event.m_x,59) && (Event.m_y == 12) )
  175.         Event.key = 'S';
  176.       else if( range(61,Event.m_x,71) && (Event.m_y == 12) )
  177.         Event.key = 'Z';
  178.       else if( range( 7,Event.m_x,47) && (Event.m_y == 13) )
  179.         Event.key = 'P';
  180.       else if( range( 7,Event.m_x,47) && (Event.m_y == 14) )
  181.         Event.key = 'F';
  182.       else if( range( 7,Event.m_x,47) && (Event.m_y == 15) )
  183.         Event.key = 'D';
  184.       else if( range( 7,Event.m_x,47) && (Event.m_y == 16) )
  185.         Event.key = 'M';
  186.  
  187.       else if( range( 21,Event.m_x,24) && (Event.m_y == 22) )
  188.         Event.key = KEY_UP;
  189.       else if( range( 26,Event.m_x,29) && (Event.m_y == 22) )
  190.         Event.key = KEY_DN;
  191.       else if( range( 32,Event.m_x,37) && (Event.m_y == 22) )
  192.         Event.key = KEY_PGUP;
  193.       else if( range( 39,Event.m_x,44) && (Event.m_y == 22) )
  194.         Event.key = KEY_PGDN;
  195.       else if( range( 47,Event.m_x,52) && (Event.m_y == 22) )
  196.         Event.key = KEY_HOME;
  197.       else if( range( 54,Event.m_x,58) && (Event.m_y == 22) )
  198.         Event.key = KEY_END;
  199.       else
  200.         Event.key = 0;  
  201.     }
  202.     switch(Event.key)
  203.     {
  204.       /*------------------------- process menus ----------------------------*/
  205.       case KEY_ALT_Q:                                       /* quit program */
  206.         end_flag = 1;
  207.         break;
  208.       case KEY_ALT_F: case KEY_ALT_E: case KEY_ALT_P:
  209.         m_show();
  210.         if( Event.key == KEY_ALT_F )
  211.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'F',M_EXIT_ON_ESC);
  212.         else if( Event.key == KEY_ALT_E )
  213.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'E',M_EXIT_ON_ESC);
  214.         else
  215.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'P',M_EXIT_ON_ESC);
  216.         switch( ret_val )
  217.         {
  218.           case 3:                                           /* quit program */
  219.             end_flag = 1;
  220.             break;
  221.           case 4:                                           /* load file    */
  222.             file_load(Customers);
  223.             break;
  224.           case 5:                                           /* save file    */
  225.             file_save(Customers);
  226.             break;
  227.           case 6:                                           /* clear one    */
  228.             setmem(cp, sizeof(CUST), 0);
  229.             break;
  230.           case 7:                                           /* clear all    */
  231.             setmem(Customers, sizeof(Customers), 0);
  232.             break;
  233.           case 9:                                           /* print one    */
  234.             if( print_stat )
  235.               print_cust(cp, &Print);
  236.             break;
  237.           case 10:                                          /* print all    */
  238.             if( print_stat )
  239.               for( i = 0; i < MAX_CUST; i++ )
  240.                 if( strlen(Customers[i].name) )             /* not empty?   */
  241.                   print_cust(&Customers[i], &Print);
  242.             break;
  243.         }
  244.         break;
  245.       /*---------------------- process cursor keys -------------------------*/
  246.       case KEY_HOME:
  247.         cust = 0;
  248.         break; 
  249.       case KEY_END:
  250.         cust = MAX_CUST-1;
  251.         break; 
  252.       case KEY_DN: 
  253.         if( cust < MAX_CUST-1 )
  254.           cust++;
  255.         break;
  256.       case KEY_UP: 
  257.         if( cust > 0 )
  258.           cust--;
  259.         break;
  260.       case KEY_PGUP: 
  261.         if( cust >= 10 )
  262.           cust -= 10;
  263.         else
  264.           cust = 0;
  265.         break;
  266.       case KEY_PGDN: 
  267.         if( cust < MAX_CUST-11 )
  268.           cust += 10;
  269.         else
  270.           cust = MAX_CUST-1;
  271.         break;
  272.       /*--------------------- process field edit keys ----------------------*/
  273.       case 'b': case 'B':                         /* get new business name  */
  274.         mv_cs( 11, 3, wnp);
  275.         wn_gets_ll(cp->business, "________________________________",
  276.                                  "********************************",
  277.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  278.         break;
  279.       case 'n': case 'N':                         /* get new contact name   */
  280.         mv_cs( 11, 4, wnp);
  281.         wn_gets_ll(cp->name, "________________________________",
  282.                              "********************************",
  283.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  284.         break;
  285.       case 'a': case 'A':                         /* get new address        */
  286.         mv_cs( 11, 5, wnp);
  287.         wn_gets_ll(cp->addr, "________________________________",
  288.                              "********************************",
  289.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  290.         break;
  291.       case 'c': case 'C':                         /* get new city           */
  292.         mv_cs( 11, 6, wnp);
  293.         wn_gets_ll(cp->city, "________________________________",
  294.                              "********************************",
  295.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  296.         break;
  297.       case 's': case 'S':                         /* get new state          */
  298.         mv_cs( 51, 6, wnp);
  299.         wn_gets_ll(cp->state, "__", "UU",
  300.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 2, wnp);
  301.         break;
  302.       case 'z': case 'Z':                         /* get new zip            */
  303.         mv_cs( 60, 6, wnp);
  304.         wn_gets_ll(cp->zip, "_____", "#####",
  305.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 5, wnp);
  306.         break;
  307.       case 'p': case 'P':                         /* get new phone number   */
  308.         mv_cs( 11, 7, wnp);
  309.         wn_gets_ll(cp->phone, "(___)___-____", " ### ### ####",
  310.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  311.         break;                    
  312.       case 'f': case 'F':                         /* get new fax number     */
  313.         mv_cs( 11, 8, wnp);
  314.         wn_gets_ll(cp->fax, "(___)___-____", " ### ### ####",
  315.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  316.         break;
  317.       case 'd': case 'D':                         /* get new date           */
  318.         mv_cs( 11, 9, wnp);
  319.         wn_gets_ll(cp->date, "__/__/__", "## ## ##", swap_nibbles(wnp->att),
  320.           G_EXIT_ON_FILL, 8, wnp);
  321.         break;
  322.       case 'm': case 'M':                         /* get new memo field     */
  323.         mv_cs( 11, 10, wnp);
  324.         wn_gets_ll(cp->memo, "________________________________",
  325.                              "********************************",
  326.           swap_nibbles(wnp->att), G_STRIP_END, 32, wnp);
  327.         break;
  328.  
  329.     }
  330.   }
  331.  
  332.   unlink_window(wnp);                     /* remove the window from screen  */
  333.   wn_destroy(wnp);
  334.   set_idle_func(NULL);                    /* remove background function     */
  335.   unlink_window(&Desk_wn);                /* remove the window from screen  */
  336.   wn_destroy(&Desk_wn);
  337.   end_clock();
  338.   end_video();                            /* clean up before we exit        */
  339.   end_printer(&Print);
  340.   end_mouse();  
  341.   return(1);
  342. }
  343. /*** end of main ***/
  344.  
  345. /**************/
  346. /* ~disp_time */
  347. /*            ***************************************************************/
  348. /*  This routine is called in the background by wait_event and will display */
  349. /*  the time once per second.  Notice the use of the global variables       */
  350. /*  Uw_timers.  There is an array of four "countdown" timers that are user  */
  351. /*  accessible.  Each "timer tic" will decrement the counts by one, until   */
  352. /*  0 is reached.  By "reloading" the timer with "Tics_per_sec", we only    */
  353. /*  display the time of day once per second.  "Tics_per_sec" is set         */
  354. /*  by init_clock.                                                          */
  355. /****************************************************************************/
  356. int disp_time( void )
  357. {
  358.   time_t t;
  359.   print_in_bkgrnd();                            /* call this to print       */
  360.   if( !Uw_timers[0] )                           /* has one second passed?   */
  361.   {
  362.     Uw_timers[0] = Tics_per_sec;                /* if so, reload timer      */
  363.     t = time(NULL);                             /* get time                 */
  364.     mv_cs(55, V_rows-1, &Desk_wn);              /* move window cursor       */
  365.     wn_st_qty(ctime(&t), 24, &Desk_wn);         /* output 24 characters     */
  366.     return(1);
  367.   }
  368.   return(0);
  369. }
  370. /*** end of disp_time ***/
  371.  
  372. /**************/
  373. /* ~disp_cust */
  374. /*            ***************************************************************/
  375. /*  This routine displays a customer in the desired window...               */
  376. /****************************************************************************/
  377. void disp_cust(CUST *cp, WINDOW *wnp)
  378. {
  379.   int r = 3;
  380.  
  381.   mv_cs( 1, r++, wnp );
  382.   wn_printf( wnp, "Business: %-32s", cp->business);
  383.   mv_cs( 1, r++, wnp );
  384.   wn_printf( wnp, "Name    : %-32s", cp->name );
  385.   mv_cs( 1, r++, wnp );
  386.   wn_printf( wnp, "Address : %-32s", cp->addr );
  387.   mv_cs( 1, r++, wnp );
  388.   wn_printf( wnp, "City    : %-32s State: %2s  Zip: %5s",
  389.     cp->city, cp->state, cp->zip );
  390.   wn_cleol(wnp);                                    /* clear to end of line */
  391.   mv_cs( 1, r++, wnp );
  392.   wn_printf( wnp, "Phone   : %-16s", cp->phone );
  393.   mv_cs( 1, r++, wnp );
  394.   wn_printf( wnp, "Fax     : %-16s", cp->fax );
  395.   mv_cs( 1, r++, wnp );
  396.   wn_printf( wnp, "Date    : %-10s", cp->date );
  397.   mv_cs( 1, r++, wnp );
  398.   wn_printf( wnp, "Memo    : %-32s", cp->memo );
  399. }
  400. /*** end of disp_cust ***/
  401.  
  402. /**************/
  403. /* ~file_load */
  404. /*            ***************************************************************/
  405. /*  This routine loads a file from disk into the customer array...          */
  406. /****************************************************************************/
  407. int file_load(CUST *customers)
  408. {
  409.   FILE *fp;
  410.   
  411.   if( get_fname(Fname) )
  412.   {
  413.     if( (fp = fopen(Fname, "rb")) != NULL )
  414.     {
  415.       fread(customers, sizeof(CUST), MAX_CUST, fp);
  416.       fclose(fp);
  417.       tone(1024,10);
  418.       return(1);
  419.     }
  420.   }
  421.   return(0);
  422. }
  423. /*** end of file_load ***/
  424.  
  425. /**************/
  426. /* ~file_save */
  427. /*            ***************************************************************/
  428. /*  This routine saves a file to disk from the customer array...            */
  429. /****************************************************************************/
  430. int file_save(CUST *customers)
  431. {
  432.   FILE *fp;
  433.   
  434.   if( get_fname(Fname) )
  435.   {
  436.     if( (fp = fopen(Fname, "wb")) != NULL )
  437.     {
  438.       fwrite(customers, sizeof(CUST), MAX_CUST, fp);
  439.       fclose(fp);
  440.       tone(1024,10);
  441.       return(1);
  442.     }
  443.   }
  444.   return(0);
  445. }
  446. /*** end of file_load ***/
  447.  
  448. /**************/
  449. /* ~get_fname */
  450. /*            ***************************************************************/
  451. /*  This routine prompts the user for a filename using a popup window...    */
  452. /****************************************************************************/
  453. int get_fname(char *fname)
  454. {
  455.   int ret_val = 1;
  456.   WINDOW wn;
  457.   
  458.   wn_create(20, 8, 60, 10, SLD_BDR, WN_POPUP, &wn);
  459.   wn_set(&wn);
  460.   wn_plst(4, 0, "Enter filename:", &wn);
  461.   if( wn_gets_ll(fname, "____________", "************",
  462.       swap_nibbles(wn.att), G_STRIP_END, 12, &wn) == KEY_ESC )
  463.     ret_val = 0;
  464.   wn_destroy(&wn);
  465.   return(ret_val);
  466. }
  467. /*** end of get_fname ***/
  468.  
  469. /***************/
  470. /* ~print_cust */
  471. /*             **************************************************************/
  472. /*  This routine prints a customer in the desired window...                 */
  473. /****************************************************************************/
  474. void print_cust(CUST *cp, PRINT *p)
  475. {
  476.   print_printf( p, "Business: %-32s\r\n", cp->business);
  477.   print_printf( p, "Name    : %-32s\r\n", cp->name );
  478.   print_printf( p, "Address : %-32s\r\n", cp->addr );
  479.   print_printf( p, "City    : %-32s State: %2s  Zip: %5s\r\n",
  480.     cp->city, cp->state, cp->zip );
  481.   print_printf( p, "Phone   : %-16s\r\n", cp->phone );
  482.   print_printf( p, "Fax     : %-16s\r\n", cp->fax );
  483.   print_printf( p, "Date    : %-10s\r\n", cp->date );
  484.   print_printf( p, "Memo    : %-32s\r\n", cp->memo );
  485.   print_char(12,p);                                     /* print form feed  */
  486. }
  487. /*** end of print_cust ***/
  488.  
  489. /*** END OF FILE ***/
  490.